<?php
namespace app\admin\controller;

use app\common\model\Renzhensiyuan as RenzhensiyuanModel;

/**
 * 寺院认证控制器
 * Class Renzhen
 * @package app\admin\controller
 */
class Renzhengsiyuan extends Base
{
    /**
     * [excel  导出列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function excel()
    {
        $param = input('param.');
        $model = new RenzhensiyuanModel;
        $user = $this->user;
        $map = [];
        try{
            if(!empty($param['start']) || !empty($param['end'])){
                $map['addtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $data = $model->getAllData($map,'id','id,tel,comname,city,addr,duty,zongpai,addtime');
            foreach($data as &$v){
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }
            vendor("PHPExcel.PHPExcel");
            $excel = new \PHPExcel();
            //Excel表格式,这里简略写了4列
            $letter = array('A','B','C','D','E','F','G','H');
            //表头数组
            $tableheader = array('ID','手机号','寺院名','地区','详细地址','职务','宗派','申请时间');
            //填充表头信息
            for($i = 0;$i < count($tableheader);$i++) {
                $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
            }
            $excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
            $excel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
            $excel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
            $excel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
            $excel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
            $excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
            $excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
            $excel->getActiveSheet()->getColumnDimension('H')->setWidth(20);

            //填充表格信息
            for ($i = 2;$i <= count($data) + 1;$i++) {
                $j = 0;
                foreach ($data[$i - 2] as $key=>$value) {
                    $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
                    $j++;
                }
            }

            //创建Excel输入对象
            $write = new \PHPExcel_Writer_Excel5($excel);
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
            header("Content-Type:application/force-download");
            header("Content-Type:application/vnd.ms-execl");
            header("Content-Type:application/octet-stream");
            header("Content-Type:application/download");;
            header('Content-Disposition:attachment;filename="功德箱申领表.xls"');
            header("Content-Transfer-Encoding:binary");
            $write->save('php://output');
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }

        writelog('寺院认证导出excel',1,$user);
        return admin_json(1,'导出成功');
    }
}